﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Collections;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Web.Controllers
{
    public class ImageGalleryController : Controller
    {
        private CloudStorageAccount storageAccount;
        private CloudBlobClient blobClient;
        private CloudBlobContainer container;
        private CloudBlob blob;

        /*public ImageGalleryController()
        {
            // retrive storage account information
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("healthcommunity"));

            // Create the blob client 
            blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container 
            container = blobClient.GetContainerReference("healthcontainer");

            // Create the container if it doesn't already exist
            container.CreateIfNotExist();

            // Make the container public so that we can hit the URLs from the web 
            container.SetPermissions(new BlobContainerPermissions{PublicAccess = BlobContainerPublicAccessType.Blob });

            //// Retrieve reference to a blob named "blobImage"
            //blob = container.GetBlobReference("blobImage");

            //// Create or overwrite the "myblob" blob with contents from a local file
            //using (var fileStream = System.IO.File.OpenRead(@"C:\Program Files\IIS Express\Images\water.jpg"))
            //{
            //    blob.UploadFromStream(fileStream);
            //}

        }*/

        private void EnsureContainerExists()
        {
            var container = this.GetContainer();
            container.CreateIfNotExist();

            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);
        }

        private CloudBlobContainer GetContainer()
        {
            // Get a handle on account, create a blob service client and get container proxy
            
            var account = CloudStorageAccount.FromConfigurationSetting("healthcommunity");
            var client = account.CreateCloudBlobClient();

            return client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ImageGallery"));
        }

        ////public IEnumerable getImage()
        ////{
        ////    new ImageGalleryController();

        ////    var item = container.ListBlobs();
        ////    return item.ElementAt(0);
      
        ////}
        //
        // GET: /ImageGallery/

        public ActionResult Index()
        {
            //new ImageGalleryController();
            try
            {
               this.EnsureContainerExists();
            }
            catch (System.Net.WebException we)
            {
                if (we.Status == System.Net.WebExceptionStatus.ConnectFailure)
                {
                    Console.WriteLine(we.Message);
                }
            }
            catch (StorageException se)
            {
                Console.WriteLine("Storage service error: " + se.Message);
            }
            var images  = this.GetContainer().ListBlobs(new BlobRequestOptions()
            {
                UseFlatBlobListing = true,
                BlobListingDetails = BlobListingDetails.All
            });
            ViewBag.Images = images;
           
            return View();
        }

        public ActionResult Upload()
        {
            this.EnsureContainerExists();
            ViewBag.Container = this.GetContainer();
            
            return View();
        }
    }
}
